#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

op=${1-""}

case "${op}" in
###
init)
    # 创建一个名为 venv 的虚拟环境目录。
    [ -d ./venv ] || python3 -m venv venv

    source ./venv/Scripts/activate
    # venv\Scripts\activate

    echo "依赖包的检查和准备..."
    grep 'aliyun' $HOME/pip/pip.conf || {
        echo "pip的镜像设置"
        cat <<EOF > $HOME/pip/pip.conf 
[global]
index-url = https://mirrors.aliyun.com/pypi/simple

[install]
trusted-host = mirrors.aliyun.com
EOF
        echo "pip镜像设置完成"
    }
    pip show nebula2-python || pip install -r requirements.txt

    echo "导入数据到nebula..."
    pushd ./shareholding-example-main
        [ -f ./data_sample/nebula-importer.log ] && rm -f ./data_sample/nebula-importer.log
        docker compose -f ./compose-importer.yaml up
    popd
    echo "导入数据在 http://localhost:7001/schema/statistic/list 可以看到统计信息"
;;
###
server)
#venv变量不对找不到包时，在独立的bash终端执行
    echo "active venv..."
    python --version|| source ./venv/Scripts/activate
    export NG_ENDPOINTS="localhost:9669"
    python ./corp-rel-backend/app.py
    echo "后端地址 http://127.0.0.1:5001/ "
;;
###
web)
    pushd ./corp-rel-frontend
        [ -d ./node_modules ] || npm install --verbose
        npm run serve
    popd
    echo "前端地址 http://localhost:8080/ "
;;

###
*)
    echo "初始化： ./run.sh init"
    echo "服务端运行： ./run.sh server"
    echo "WEB运行： ./run.sh web 默认会有一个效果，输入查询的公司id，可以查看其关系"
;;
esac

echo "done"